<template>
    <div class="components-container">
        <div class="header-form">
          <div class="layui-inline" >
            <label class="layui-form-label">接尸日期</label>
            <div class="layui-inline">
             <el-radio-group v-model="selectDataExtend.regDateValue" @change="btnSearch">
              <el-radio-button  :label="DateSelectMode.now" class="radio">当日</el-radio-button>
              <el-radio-button  :label="DateSelectMode.future" class="radio">以后</el-radio-button>
              <el-radio-button  :label="DateSelectMode.all" class="radio">全部</el-radio-button>
             </el-radio-group>
            </div>
          </div>
          <div class="layui-inline" >
            <label class="layui-form-label">业务编号</label>
            <div class="layui-inline">
            <el-input  v-model="selectData.RegbussnumLike" placeholder="请输入业务编号"></el-input>
            </div>
          </div>
          <el-button type="primary" @click="btnSearch" :loading="this.$store.state.isloading">搜索</el-button>
          <el-button  @click="btnRepeat" >重置</el-button>
        </div>
        <div style="height:56px">
            <ul class="toolList">
              <li><el-button type="primary" @click="btnSearch" :loading="this.$store.state.isloading">刷新</el-button></li>
                <li v-if="btnShow(item.BTNVALUE)" v-for="(item, index) in $store.state.btnListCommon" @click="operate(item.BTNVALUE)">
                  <el-button type="primary" :value="item.BTNSHOWNAME" v-text="item.BTNSHOWNAME"></el-button>
                </li>
            </ul>
        </div>
        <dataTable :headList="headList" :dataList="dataList" :pageOrder="pageOrder" @pageChange="pageChange" @selectRow="selectRow"></dataTable>
       <sweet-modal ref="checkout" class="dialog">
        <h3>{{title}}</h3>
          <div style="height:50px;">
            <div class="layui-form-item">
              <div class="layui-inline">
                <label class="layui-form-label">付款方式</label>
                <div class="layui-input-inline">
                  <el-select v-model="CHDCODNAME">
                    <el-option v-for="item in payment" :key="item.CodeValue" :label="item.CodeName" :value="item.CodeValue">
                    </el-option>
                  </el-select>
                </div>
              </div>
            </div>
          </div>
          <div class="layui-form-item btnCenter">
            <el-button @click="cancel" >取消</el-button>
            <el-button type="primary" :loading="loading" @click="submit">结账</el-button>
          </div>
       </sweet-modal>
       <costTwoAdd ref="checkoutAdd" @refreshData="getData"></costTwoAdd>
       <printmodal ref="printPdf"></printmodal>
    </div>
</template>

<style rel="stylesheet/scss" lang="scss" scoped>
@import "src/styles/common.scss";
.el-button + .el-button {
  margin-left: 0;
}
.btnCenter {
  text-align: center;
}
</style>
<script>
import sweetModal from "@/components/sweetModal";
import dataTable from "@/components/Table/DataTable";
import costTwoAdd from "./costTwoAdd";
import apiBase from "@/api/apiBase.js";
import { formatDate, timeStringToDate } from "@/utils/date.js";
import { manage } from "@/mixins/managePage.js";
import { DATESELECTMODE } from "@/const/const.js";
import { ORDER } from "@/const/orderConst.js";
import { PRINT } from "@/components/Pdf/PDF.js";
import printmodal from "@/components/Pdf/Print";


const MODIFY = "调整收费项目";
const INFO = "详情";
const BILL = "挂账";
const CHECKOUT = "结账";
const CANCEL = "取消结账";
const INVALID = "作废";
const WASTE = "坏账";

const WJZ = "A";
const YJZ = "B";
const YZF = "C";
const YGZ = "D";
const HZ = "E";

export default {
  name: "checkout",
  mixins: [manage],
  data() {
    return {
      DateSelectMode: DATESELECTMODE,
      //查询条件
      selectData: {},
      selectDataExtend: {
        regDateValue: null
      },
      //收费信息
      chdData: {},
      //付款方式
      payment: [],
      CHDCODNAME: null,
      chlList: []
    };
  },
  methods: {
    //获取表头
    getColumns() {
      let data = {};
      apiBase.DoService(
        data,
        apiBase.getApiAdress().Column.GetCostColumns,
        res => {
          this.headList = res.data.content;
        }
      );
    },
    //获取收费信息
    getData() {
      let selectData = JSON.parse(JSON.stringify(this.selectData));
      if (!this.selectData.RegbussnumLike) {
        selectData.RegbussnumLike = null;
      }
      let data = {
        PageIndex: this.pageOrder.currentPage,
        PageSize: this.pageOrder.pageSize,
        OrderBy: this.pageOrder.orderType,
        OrderByType: this.pageOrder.orderSort,
        Data: selectData
      };
      apiBase.DoService(data, apiBase.getApiAdress().CostTwo.Query, res => {
        this.dataList = res.data.content.OutData;
        this.pageOrder.totalNum = res.data.content.RecordCount;
        this.dataList.forEach(data => {
          data.REGDATE = data.REGDATE.substring(0, 16);
        });
      });
    },
    //获取付款方式
    getPayment() {
      let data = {};
      apiBase.DoService(data, apiBase.getApiAdress().Code.GetPayment, res => {
        this.payment = res.data.content;
      });
    },
    //功能操作
    operate(order) {
      this.order = order;
      this.dataRepeat();
      switch (this.order) {
        case MODIFY:
          this.dataModify();
          break;
        case INFO:
          this.dataInfo();
          break;
        case BILL:
          this.chdBill();
          break;
        case CHECKOUT:
          this.$refs.checkoutAdd.open(ORDER.CHECKOUT, this.currentRow.CHDID);//结账
          break;
        case CANCEL:
          this.chdCancel();
          break;
        case INVALID:
          this.chdInvalid();
          break;
        case WASTE:
          this.chdWaste();
          break;
        case PRINT.PDFBILL://打印收费单
          this.$refs.printPdf.open(this.order, this.currentRow.CHDID);
          break;
        case PRINT.PDFAGREEMENT://打印项目协议书
          this.$refs.printPdf.open(this.order, this.currentRow.CHDID);
          break;
        case ORDER.PTCRFS://打印火化登记表
          this.$refs.printPdf.open(this.order, this.currentRow.CHDID);
          break;
        case ORDER.PRINTSLS://打印收费清单
          this.$refs.printPdf.open(this.order, this.currentRow.CHDID);
          break;
        case ORDER.PROVE://打印火化证明
          this.$refs.printPdf.open(this.order, this.currentRow.CHDREGID);
          break;
        default:
      }
    },
    //收费信息修改
    dataModify() {
      this.$refs.checkoutAdd.open(ORDER.CHEMODIFY, this.currentRow.CHDID);
    },
    //提交
    submit() {
      switch (this.order) {
        case CHECKOUT:
          this.chdCheckout();
          break;
        default:
      }
    },
    //收费详情
    dataInfo() {
      this.$refs.checkoutAdd.open(ORDER.INFO, this.currentRow.CHDID);
    },
    //挂账
    chdBill() {
      this.$confirm("确认是否挂账?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(callback => {
          let data = {
            CHDID: this.currentRow.CHDID
          };
          apiBase.DoService(data, apiBase.getApiAdress().Checkout.Bill, res => {
            this.$message({
              type: "success",
              message: "挂账成功!"
            });
            this.getData();
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消"
          });
        });
    },
    //结账
    chdCheckout() {
      let data = {
        CHDID: this.currentRow.CHDID,
        CHDCODNAME: this.CHDCODNAME
      };
      apiBase.DoService(data, apiBase.getApiAdress().Checkout.Checkout, res => {
        this.$message({
          type: "success",
          message: "结账成功!"
        });
        this.getData();
        this.$refs.checkout.close();
      });
    },
    //取消结账
    chdCancel() {
      this.$confirm("确认是否取消结账?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(callback => {
          let data = {
            CHDID: this.currentRow.CHDID
          };
          apiBase.DoService(
            data,
            apiBase.getApiAdress().Checkout.CancelCheckout,
            res => {
              this.$message({
                type: "success",
                message: "取消结账成功!"
              });
              this.getData();
            }
          );
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消"
          });
        });
    },
    //作废
    chdInvalid() {
      this.$confirm("确认是否作废?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(callback => {
          let data = {
            CHDID: this.currentRow.CHDID
          };
          apiBase.DoService(
            data,
            apiBase.getApiAdress().Checkout.Invalid,
            res => {
              this.$message({
                type: "success",
                message: "作废成功!"
              });
              this.getData();
            }
          );
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消"
          });
        });
    },
    //坏账
    chdWaste() {
      this.$confirm("确认是否坏账?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(callback => {
          let data = {
            CHDID: this.currentRow.CHDID
          };
          apiBase.DoService(
            data,
            apiBase.getApiAdress().Checkout.Waste,
            res => {
              this.$message({
                type: "success",
                message: "坏账成功!"
              });
              this.getData();
            }
          );
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消"
          });
        });
    },
    //查询条件重置
    btnRepeat() {
      this.selectData = {};
      this.initSeachdata();
      this.getData();
    },
    dataRepeat() {
      this.chdData = {};
    },
    //按钮显隐
    btnShow(btn) {
      let status = null;
      if (this.currentRow) {
        status = this.currentRow.CHDSTATUS;
      }
      switch (btn) {
        case MODIFY:
          return status === WJZ || status === YGZ;//调整收费项目
          break;
        case INFO:
          return status;//详情
          break;
        case BILL:
          return status === WJZ;//挂账
          break;
        case CHECKOUT:
          return status === WJZ || status === YGZ;//结账
          break;
        case CANCEL:
          return status === YJZ;//取消结账
          break;
        case INVALID:
          return status === YJZ;//作废
          break;
        case WASTE:
          return status === YGZ;//坏账
          break;
        case ORDER.PTCRFS:
          if(status == "B")
          return true;//打印火花登记单
          break;
        case ORDER.PRINTSLS:
          if(status == "B")
          return true;//打印收费清单
          break;
        case ORDER.PROVE:
          if(status == "B")
          return true;//打印火化证明
          break;
        default:
          return status;
      }
    },
    //关闭
    checkoutClose() {
      this.$refs.checkout.close();
    },
    initSeachdata() {
      this.selectDataExtend.regDateValue = DATESELECTMODE.now;
      this.selectData.RegDateOrChdDate = formatDate(new Date(), "yyyyMMdd");
    }
  },
  components: {
    sweetModal,
    dataTable,
    costTwoAdd,
    printmodal
  },
  activated() {
    this.getData();
  },
  mounted() {
    this.initSeachdata();
  },
  created() {
    this.initSeachdata();
    //this.getData();
    this.getColumns();
    this.getPayment();
    console.log("btns", this.$store.state.btnListCommon);
  },
  watch: {
    "selectDataExtend.regDateValue": function(val) {
      this.selectData.RegdateGreater = null;
      this.selectData.RegDateOrChdDate = null;
      switch (val) {
        case DATESELECTMODE.now:
          this.selectData.RegDateOrChdDate = formatDate(new Date(), "yyyyMMdd");
          break;
        case DATESELECTMODE.future:
          this.selectData.RegdateGreater = formatDate(new Date(), "yyyyMMdd");
          break;
        default:
          break;
      }
    }
  }
};
</script>

